/**
 * \file	Hache.cpp
 * \brief	Implémentatin du foncteur utilisé par Table.
 * \author	Thibaut ALGRIN
 * \author	Emmanuel CARRE
 * \version	0.1
 * \date	Décembre 2011
 */

#include "Hache.h"
#include "Prime.h"

using namespace tp3;

/*
 * \brief Constructeur de la classe Hache
 * \fn Hache(unsigned int size = 100)
 * \param unsigned int
 */
Hache::Hache(unsigned int size)
: size(size)
{
}
/*
 * \brief Destructeur de la classe Hache
 */
Hache::~Hache()
{
}
/*
 * \brief Surcharge de l'operateur ()
 * \fn unsigned int operator() (const int			& clef)	const
 * \param const int
 * \return unsigned int
 */
unsigned int Hache::operator() (const int	& cle)	const
{
	return ((919 - (cle % this->size)) % this->size);
}

/*
 * \brief Surcharge de l'operateur ()
 * \fn unsigned int operator() (const double			& clef)	const
 * \param const double
 * \return unsigned int
 */
unsigned int Hache::operator() (const double	&cle)	const
{
	double tmp = cle / this->size;
	double tmp2;

	tmp = cle - (this->size * tmp);
	tmp2 = (919 - tmp) / this->size;
	tmp2 = (919 - tmp) - (this->size * tmp2);
	return (tmp2);
}
/*
 * \brief Surcharge de l'operateur ()
 * \fn unsigned int operator() (const char			& clef)	const
 * \param const char
 * \return unsigned int
 */
unsigned int Hache::operator() (const char	& cle)	const
{
	return ((919 - (cle % this->size)) % this->size);
}
/*
 * \brief Surcharge de l'operateur ()
 * \fn unsigned int operator() (const std::string			& clef)	const
 * \param const std::string
 * \return unsigned int
 */
unsigned int Hache::operator() (const std::string	& cle)	const
{
	const unsigned long GRAND_PREMIER = 1000003;
	unsigned long total = 0;
	unsigned long premiers[] = {11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79};
	for(unsigned int i = 0; i < cle.length(); i++)
		total += premiers[i] * cle[i];
	return total * GRAND_PREMIER;
}
